package com.falser.order.mq;

import com.alibaba.fastjson.JSON;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.falser.order.alipay.AlipayTrade;
import com.falser.order.entity.Order;
import com.falser.order.enums.OrderStatusEnum;
import com.falser.order.enums.PayStatusEnum;
import com.falser.order.mq.dto.ParkingOrderDTO;
import com.falser.order.service.OrderService;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;

/**
 * 订单贸易状态mq消费者
 *
 * @author falser
 * @date 2023/03/29
 */
@Component
@Slf4j
public class OrderTradeStatusMqConsumer {

	private final OrderService orderService;
	/**
	 * 支付宝交易
	 */
	private final AlipayTrade alipayTrade;

	private final ObjectMapper objectMapper = new ObjectMapper()
			.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

	public OrderTradeStatusMqConsumer(OrderService orderService, AlipayTrade alipayTrade) {
		this.orderService = orderService;
		this.alipayTrade = alipayTrade;
	}

	@RabbitListener(queues = "${mq.order.trade.status.queue}", containerFactory = "myContainerFactory")
	public void dealMq(String data, Message message, Channel channel) throws IOException {
		log.info("停车后台开始消费订单支付成功消息:data:{},tag:{}", data, message.getMessageProperties().getDeliveryTag());
		try {
			ParkingOrderDTO orderDTO = objectMapper.readValue(data, ParkingOrderDTO.class);
			Order order = orderService.getById(orderDTO.getId());
			if (orderDTO.getTimes().equals(20)) {
				// 撤销交易
				alipayTrade.tradeCancel(order.getOrderNo());
				order.setStatus(OrderStatusEnum.CANCEL.getValue());
				order.setPayStatus(PayStatusEnum.TRADE_CLOSED.name());
				orderService.updateById(order);
				return;
			}
			AlipayTradeQueryResponse response = alipayTrade.query(order.getOrderNo());
			log.info("交易信息:{}", JSON.toJSONString(response));
			if (Objects.equals(PayStatusEnum.TRADE_SUCCESS.name(), response.getTradeStatus())) {
				order.setPayStatus(PayStatusEnum.TRADE_SUCCESS.name());
				order.setStatus(OrderStatusEnum.COMPLETED.name());
			} else if (Objects.equals(PayStatusEnum.WAIT_BUYER_PAY.name(), response.getTradeStatus())) {
				order.setPayStatus(PayStatusEnum.WAIT_BUYER_PAY.name());
				order.setStatus(OrderStatusEnum.ONGOING.name());

			} else if (Objects.equals(PayStatusEnum.TRADE_CLOSED.name(), response.getTradeStatus())) {
				order.setPayStatus(PayStatusEnum.TRADE_CLOSED.name());
				order.setStatus(OrderStatusEnum.CLOSED.name());
			} else if (Objects.equals(PayStatusEnum.TRADE_FINISHED.name(), response.getTradeStatus())) {
				order.setPayStatus(PayStatusEnum.TRADE_FINISHED.name());
				order.setStatus(OrderStatusEnum.COMPLETED.name());
				// 释放车位
			}
			orderService.updateById(order);
		} catch (Exception e) {
			log.error("CRM消费订单支付成功消息失败!,错误:{}", e.getMessage());
			e.printStackTrace();
		} finally {
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		}
	}
}
